"use strict";
var _node2, _menuID;
var $structure, $selectPeriod, $selectIndex;

function init() {
	$structure = $('#structure');
	$selectPeriod = $('#selectPeriod');
	$selectIndex = $('#selectIndex');

	getyear();
	getCheckStat("status");
	initPeriod();
}

function initPeriod() {
	$selectIndex.select2({
		placeholder: "请选择...",
		language: "zh-CN"
	});

	$selectPeriod.select2({
		placeholder: "请选择...",
		language: "zh-CN"
	});

	vw_common.customAjax({
		url: VW_INTERFACE.WEBSERVICE.TableManage('GetTableData'),
		type: "GET",
		async: false,
		dataType: "json",
		data: {
			table: "TB_BIZ_SURVEY"
		}
	}, function(e) {
		var f_name, f_level;
		$.each(e.column, function(k, val) {
			if(val == "F_NAME") {
				f_name = k;
			}
			if(val == "PK_ID") {
				f_level = k;
			}
		});

		$.each(e.data, function(k, val) {
			$selectPeriod.append('<option value=' + val[f_level] + '>' + val[f_name] + '</option>');
		});

		$selectIndex.on('select2:select', function(e) {
			InitTable();
		});

		$selectPeriod.on('select2:select', function(e) {
			InitTable();
		});
		$selectPeriod.val(e.data[0][f_level]).trigger('select2:select');
	});
}

function InitTable() {
	var custom = [
		'<div class="row">',
		'	<div class="col-md-12">',
		'		<button type="button" id="btn_export" class="btn btn-primary pull-left" style="margin:0 10px 5px 0"><i class="fa fa-download"></i>  导出</button>',
		'		<button type="button" class="btn btn-info pull-left" style="margin:0 10px 5px 0">导入权数推算</button>',
		'		<div id="statmsg" class="pull-right" style="padding: 5px 10px;"></div>',
		'		<table id="vw-treetable"></table>',
		'	</div>',
		'</div>'
	].join('');

	_menuID = 1;

	$structure.vw_structure('destroy').vw_structure({
		showEdittable: false,
		customTab: custom,
		searchtree: {
			menu: [{
				id: 1,
				text: "按对象分类"
			}, {
				id: 2,
				text: "按作物类型分类"
			}],
			showEdit: false,
			onTreeSelected: function(node) {
				_node2 = node;
				ReadStationData($('#vw-treetable'), $('#statmsg'), _node2)
			},
			onMenuChanged: function(menu) {
				_menuID = menu;
			}
		},
		interfaceAddress: VW_INTERFACE.WEBSERVICE.DataAggregate,
		onInitialized: function() {
			$('#btn_export').on('click', function() {
				downloadTable($('#vw-treetable')[0], _node2.text);
			});
		}
	});
}

function ReadStationData($treetable, $StatMsg, node) {
	require(["bootstrap.table-cn", 'bootstrap.table.treegrid'], function() {
		vw_common.customAjax({
			url: VW_INTERFACE.WEBSERVICE.DataAggregate('LoadData'),
			type: 'GET',
			dataType: 'json',
			data: {
				"table": node.id,
				"type": _menuID,
				"index": $selectIndex.val(),
				"level": $selectPeriod.val(),
				"year": _year.currentYear
			},
		}, function(e) {
			e.column.splice(0, 2);
			$treetable.bootstrapTable('destroy').bootstrapTable({
				data: e.data,
				columns: e.column,
				//				treeShowField: "F_NAME",
				//				idField: "PK_ID",
				//				parentIdField: "F_PID",
				height: $(window).height() - 180,
			})

			//			$treetable.treegrid({
			//				initialState: 'expanded',
			//				treeColumn: 0,
			//				expanderExpandedClass: 'glyphicon glyphicon-chevron-down',
			//				expanderCollapsedClass: 'glyphicon glyphicon-chevron-right'
			//			})
			//
			//			$treetable.find('td:nth-child(1) span').addClass('pull-left');
			//			$treetable.find('td:nth-child(1) div').addClass('pull-left');
		});
	});
}

function downloadTable(table, filename) {
	require(['xlsx', 'FileSaver'], function() {
		var workbook = XLSX.utils.table_to_book(table);
		var wbout = XLSX.write(workbook, {
			bookType: 'xlsx',
			bookSST: false,
			type: 'binary'
		});

		saveAs(new Blob([s2ab(wbout)], {
			type: ""
		}), filename + ".xlsx");

		function s2ab(s) {
			var buf = new ArrayBuffer(s.length);
			var view = new Uint8Array(buf);
			for(var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
			return buf;
		}
	})
}